<?php 
namespace app\app\model;
use think\Model;
use think\Db;

class Orders extends Model{

	public $user_id;
	public $id;

	//获取所有订单
	public function getAll($status)
	{
		$status = $status;	
		$where['a.user_id'] = array('eq',$this->user_id);
		if( $status > 0 ){
			$where['a.status'] = array('eq',$status);
		}else{
			$where['a.status'] = array('neq',0);
		}
		$data = $this->alias('a')->join('store b','a.store_id=b.id','left')->field('a.id,a.order_number,a.fee,b.name,b.phone,a.create_time')->where($where)->select();
		return $data;
	}

	public function getOrderByNumber($number)
	{
		$data = $this->where('order_number','eq',$number)->find();
		return $data;
	}

	public function getUnpaid(){
		$where['a.user_id'] = array('eq',$this->user_id);
		$where['a.status']  = array('eq',1);
		$data = $this->alias('a')->field('a.id')->where($where)->count();
		return $data;
	}

	public function getOne($id){
		$where['a.id'] = array('eq',$id);
		$where['a.user_id'] = array('eq',$this->user_id);
		$order = $this->alias('a')->join('store b','a.store_id=b.id','left')->field('a.id,a.order_number,a.p_id,a.fee,b.name,b.phone,a.create_time,a.status,a.coupon_id')->where($where)->find();
		//菜单
		$pocket = Db::name('pocket')->field('a.num,a.price,b.name')->alias('a')->join('goods b','a.goods_id=b.id','left')->where('a.id','in',$order['p_id'])->select();
		unset($order['p_id']);
		$order['pocket'] = $pocket;
		//优惠券
		if($order['coupon_id'] != 0){
			$order['coupon'] = Db::name('user_coupon')->alias('a')->join('coupon b','a.coupon_id=b.id','left')->field('a.id,b.exempt,b.max')->where('a.id','eq',$order['coupon_id'])->find();
		}else{
			$order['coupon'] = [];
		}
		unset($order['coupon_id']);
		return $order;
	}

}